import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class UiAvatar extends StatelessWidget {
  const UiAvatar({
    super.key,
    this.url,
    this.assetUrl,
    this.size = 40,
    this.radius = 5,
    this.round = true,
    this.backgroundColor,
    this.border,
    this.placeholder,
    this.margin,
    this.child,
    this.boxShadow,
  });
  final String? url;
  final String? assetUrl;
  final double size;
  final double radius;
  final bool round;
  final Color? backgroundColor;
  final BoxBorder? border;
  final Widget? placeholder;
  final EdgeInsetsGeometry? margin;
  final Widget? child;
  final List<BoxShadow>? boxShadow;
  @override
  Widget build(BuildContext context) {
    BorderRadius borderRadius =
        BorderRadius.circular(round ? size / 2 : radius);

    return Container(
      height: size,
      width: size,
      margin: margin,
      constraints: BoxConstraints(maxWidth: size, maxHeight: size),
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
        border: border,
        color: backgroundColor,
        borderRadius: borderRadius,
        boxShadow: boxShadow,
      ),
      child: ClipRRect(
        borderRadius:
            borderRadius - BorderRadius.circular(border?.top.width ?? 0),
        child: child ?? imageView(),
      ),
    );
  }

  Widget imageView() {
    if (url == '' || url == null) {
      return defaultView();
    }
    return CachedNetworkImage(
      imageUrl: url.toString(),
      fit: BoxFit.cover,
      // colorBlendMode: BlendMode.color,
      // color: Colors.grey,
      progressIndicatorBuilder: (context, url, error) {
        return defaultView();
      },
      errorWidget: (context, url, error) {
        return defaultView();
      },
    );
  }

  Widget defaultView() {
    return placeholder ??
        Image.asset(
          assetUrl ?? 'res/images/launcher_icon.png',
          fit: BoxFit.cover,
        );
  }
}
